using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class ChiTietPhieuKhamDAO
    {
        public static bool Insert(ChiTietPhieuKhamDTO ct)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
                sqlparam.Add(new SqlParameter("@MaChiTietPhieuKham", ct.MaChiTietPhieuKham));
                sqlparam.Add(new SqlParameter("@MaCachDung", ct.MaCachDung));
                sqlparam.Add(new SqlParameter("@MaDonVi", ct.MaDonVi));
                sqlparam.Add(new SqlParameter("@MaPhieuKham", ct.MaPhieuKham));
                sqlparam.Add(new SqlParameter("@MaThuoc", ct.MaThuoc));
                sqlparam.Add(new SqlParameter("@SoLuong", ct.SoLuong));
                sqlparam.Add(new SqlParameter("@ThanhTien", ct.ThanhTien));
                sqlparam.Add(new SqlParameter("@DonGiaHienTai", ct.DonGiaHienTai));
                int n = SqlDataAccessHelper.ExecuteNoneQuery("spChiTietPhieuKhamInsert",sqlparam);
                if (n == 1)
                    kq = true;

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static bool Delete(ChiTietPhieuKhamDTO ct)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
                sqlparam.Add(new SqlParameter("@MaChiTietPhieuKham", ct.MaChiTietPhieuKham));
                
                int n = SqlDataAccessHelper.ExecuteNoneQuery("spChiTietPhieuKhamDelete", sqlparam);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static bool Update(ChiTietPhieuKhamDTO ct)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
                sqlparam.Add(new SqlParameter("@MaChiTietPhieuKham", ct.MaChiTietPhieuKham));
                sqlparam.Add(new SqlParameter("@MaCachDung", ct.MaCachDung));
                sqlparam.Add(new SqlParameter("@MaDonVi", ct.MaDonVi));
                sqlparam.Add(new SqlParameter("@MaPhieuKham", ct.MaPhieuKham));
                sqlparam.Add(new SqlParameter("@MaThuoc", ct.MaThuoc));
                sqlparam.Add(new SqlParameter("@SoLuong", ct.SoLuong));
                sqlparam.Add(new SqlParameter("@ThanhTien", ct.ThanhTien));
                sqlparam.Add(new SqlParameter("@DonGiaHienTai", ct.DonGiaHienTai));
                int n = SqlDataAccessHelper.ExecuteNoneQuery("spChiTietPhieuKhamUpdate", sqlparam);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static List<ChiTietPhieuKhamDTO> Select()
        {
            List<ChiTietPhieuKhamDTO> lst = new List<ChiTietPhieuKhamDTO>();
            try
            {
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spChiTietPhieuKhamSelect");
                foreach (DataRow dr in dt.Rows)
                {
                    ChiTietPhieuKhamDTO ct = new ChiTietPhieuKhamDTO();
                    ct.DonGiaHienTai = float.Parse(dr["DonGiaHienTai"].ToString());
                    ct.MaCachDung = int.Parse(dr["MaCachDung"].ToString());
                    ct.MaChiTietPhieuKham = int.Parse(dr["MaChiTietPhieuKham"].ToString());
                    ct.MaDonVi = int.Parse(dr["MaDonVi"].ToString());
                    ct.MaPhieuKham = int.Parse(dr["MaPhieuKham"].ToString());
                    ct.MaThuoc = int.Parse(dr["MaThuoc"].ToString());
                    ct.SoLuong = int.Parse(dr["SoLuong"].ToString());
                    ct.ThanhTien = float.Parse(dr["ThanhTien"].ToString());
                    lst.Add(ct);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lst;
        }
        public static ChiTietPhieuKhamDTO SelectByID(int MaChiTietPhieuKham)
        {
            ChiTietPhieuKhamDTO ct = new ChiTietPhieuKhamDTO();
            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
                sqlparam.Add(new SqlParameter("@MaChiTietPhieuKham", MaChiTietPhieuKham));
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spChiTietPhieuKhamSelectByID",sqlparam);
                DataRow dr = dt.Rows[0];
                
                
                ct.DonGiaHienTai = float.Parse(dr["DonGiaHienTai"].ToString());
                ct.MaCachDung = int.Parse(dr["MaCachDung"].ToString());
                ct.MaChiTietPhieuKham = int.Parse(dr["MaChiTietPhieuKham"].ToString());
                ct.MaDonVi = int.Parse(dr["MaDonVi"].ToString());
                ct.MaPhieuKham = int.Parse(dr["MaPhieuKham"].ToString());
                ct.MaThuoc = int.Parse(dr["MaThuoc"].ToString());
                ct.SoLuong = int.Parse(dr["SoLuong"].ToString());
                ct.ThanhTien = float.Parse(dr["ThanhTien"].ToString());
                    
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ct;
        }
    }
}


